package com.dranie.action;

import com.dranie.entity.User;
import com.dranie.interceptor.UserInterceptor;
import com.dranie.validator.UserValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

/**
 * BlogController 注意：在实际项目中业务与sql需要写在Model中，此demo仅为示意,故将sql写在了Controller中
 */
@Before(UserInterceptor.class)
public class UserController extends Controller {
    public void index() {
        setAttr("userPage", User.dao.paginate(getParaToInt(0, 1), 10,
                "select *", "from user order by id asc"));
        render("list.html");
    }

    public void add() {
    }

    @Before(UserValidator.class)
    public void save() {
        getModel(User.class).save();
        redirect("/admin/user");
    }

    public void edit() {
        setAttr("user", User.dao.findById(getParaToInt()));
    }

    @Before(UserValidator.class)
    public void update() {
        getModel(User.class).update();
        redirect("/admin/user");
    }

    public void delete() {
        User.dao.deleteById(getParaToInt());
        redirect("/admin/user");
    }
}
